# -*- coding:utf-8 -*-
import logging
import time
from django.utils.deprecation import MiddlewareMixin

# 日志
logger = logging.getLogger('autotest')

MAX_REQUEST_PER_SECOND = 1000  # 每秒访问次数


# 限制访问频率的中间件
class RequestBlockingMiddleware(MiddlewareMixin):
    def process_request(self, request):
        now = time.time()
        request_queue = request.session.get('request_queue', [])
        if len(request_queue) < MAX_REQUEST_PER_SECOND:
            request_queue.append(now)
            request.session['request_queue'] = request_queue
        else:
            time0 = request_queue[0]
            if (now - time0) < 1:
                time.sleep(5)
                logger.info("命中限制访问频率的中间件，time.sleep(5)")

            request_queue.append(time.time())
            request.session['request_queue'] = request_queue[1:]
